package com.kun.base.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import java.util.*

/**
 * Created by ZYG on 2017/6/23.
 * Viewpager+Fragment的适配器 绑定TabLayout
 */
class MyFragmentPagerAdapter(
    fm: FragmentManager,
    fragments: List<Fragment>,
    titles: List<String>
) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
    private var fragments: List<Fragment> =
        ArrayList()


    private var titles: List<String>

    private val fm: FragmentManager

    fun setNewData(
        fragments: List<Fragment>,
        titles: List<String>
    ) {
        this.fragments = fragments
        this.titles = titles
    }

    override fun getItem(position: Int): Fragment {
        return fragments[position]
    }

    override fun getCount(): Int {
        return fragments.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return if (!titles.isEmpty()) {
            titles[position]
        } else super.getPageTitle(position)
    }

    init {
        this.fragments = fragments
        this.titles = titles
        this.fm = fm
    }
}